<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app"></div>

    <script src="./src/react.js"></script>
    <script src="./src/react-dom.js"></script>
    <script src="./src/babel.js"></script>

    <script type="text/babel">

        class Curd extends React.Component {
            constructor() {
                super();
                this.state = {
                    list: [
                        { id: 1, user: "gao", age: 18 },
                        { id: 2, user: "gao2", age: 19 },
                        { id: 3, user: "gao3", age: 29 },
                        { id: 4, user: "gao", age: 18 },
                    ],
                    user:"1",
                    age:"2",
                    editId:"",
                    flag:true

                }

            }

            changeInput=(event)=>{
                this.setState({
                    [event.target.name]:event.target.value
                })
            }

            add=()=>{
                // console.log(this.state.user, this.state.age)
                let obj= {id:+new Date(),user: this.state.user, age: this.state.age }
                let list =  JSON.parse ( JSON.stringify(this.state.list));

                    list.push( obj );

                    this.setState({
                        list:list
                    })

            }

            del=(id)=>{
                let list =  JSON.parse( JSON.stringify(this.state.list));
                let index = list.findIndex(v=>v.id == id);
                list.splice(index,1);

                this.setState({
                        list:list
                })

            }
            edit=(id)=>{
                let list =  JSON.parse( JSON.stringify(this.state.list));
                let obj = list.find(v=>v.id == id);

                this.setState({
                    user:obj.user,
                    age:obj.age,
                    editId:obj.id,
                    flag:false
                })
            }
            editOK=()=>{
                let list =  JSON.parse( JSON.stringify(this.state.list));
                let index = list.findIndex(v=>v.id == this.state.editId);
                list[index].user= this.state.user;
                list[index].age= this.state.age;

                this.setState({
                        list:list,
                        flag:true
                })

            }


            render() {
            
                return (
                    <div>
                        {/* 添加表单  */}
                        <form >
                            <div>
                                <input type="text" name="user" value={this.state.user}  onChange={this.changeInput} />
                            </div>
                            <div>
                                <input type="text" name="age" value={this.state.age}  onChange={this.changeInput}/>
                            </div>
                            <div>
                                { 
                                    this.state.flag ?  <input type="button" value="提交" onClick={this.add}/>:<input type="button" value="确认修改" onClick={this.editOK}/>
                                }
                               
                            
                            </div>
                        </form>

                        {/* 展示表格 */}

                        <table border="1">
                            <thead>
                                <tr>
                                    <th>姓名</th>
                                    <th>年龄</th>
                                    <th>操作</th>
                                </tr>
                            </thead>

                            <tbody>
                                {
                                    this.state.list.length >0 ? this.state.list.map(v => {
                                        return (
                                            <tr key={v.id}>
                                                <td>{v.user}</td>
                                                <td>{v.age}</td>
                                                <td>
                                                    <button onClick={()=>this.del(v.id)}>删除</button>
                                                    <button onClick={()=>this.edit(v.id)}>修改</button>

                                                </td>
                                            </tr>

                                        )
                                    }):<tr><th colSpan="3">暂无数据</th></tr>
                                }


                            </tbody>
                        </table>

                    </div>
                )
            }
        }




        ReactDOM.render(
            <div>
                <Curd></Curd>
            </div>,
            document.getElementById("app")
        )

    </script>

</body>

</html>